-- UISpellOperation
-- Create by luohj Jan/14/2015
-- 魔法卷轴操作界面

require "game/ui/form/spell/UISelectTarget"

-- UISpellOperation继承自Layer
UISpellOperation = class("UISpellOperation", function()
    return cc.Layer:create();
end);

function UISpellOperation.create(spellId, onlyInfo)
    return UISpellOperation.new(spellId, onlyInfo);
end

-- 构造函数
function UISpellOperation:ctor(spellId, onlyInfo)
    -- 初始化
    self:setName("UISpellOperation");

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    local node = cc.CSLoader:createNode("layout/spell/magic_operate.csb");
    self:addChild(node);
    self.node = node;
    local CT = node:getChildByName("CT");
    self.CT = CT;

    self.classId = spellId;

    self.onlyInfo = onlyInfo;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 初始化
function UISpellOperation:redraw()
    local spellInfo = SpellM.query(self.classId);

    if spellInfo == nil then
        error("获取不到卷轴(%d)信息", self.classId);
        return;
    end

    -- 设置魔法名称
    local nameLabel = self.CT:getChildByName("name");
    local nameText = spellInfo["desc"];
    TextStyleM.setTitleStyle(nameLabel);
    nameLabel:setString(nameText);

    -- 设置魔法效果
    local effectLabel = self.CT:getChildByName("effect");
    local skillId = SpellM.query(self.classId, "cast_skill_id");
    local effectText = SkillM.getSkillDesc(skillId, false, "append");
    TextStyleM.setTextStyle(effectLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    effectLabel = applyString(effectLabel, effectText);

    -- -- 设置剩余卷轴的个数
    local count = ItemM.getAmount(ME.user,self.classId);

    -- 加入魔法卷轴剩余个数为0 则将使用和分解按钮设置为禁用状态
    if count <= 0 then
        local btnUse = self.CT:getChildByName("btn_resolve");
        btnUse:setBright(false);
        btnUse:setTouchEnabled(false);

        local btnResolve = findChildByName(self.node, "BT/btn_resolve");
        btnResolve:setBright(false);
        btnResolve:setTouchEnabled(false);
    end

    local btnResolve = findChildByName(self.node, "CT/btn_resolve");
    if self.onlyInfo == true then
        btnResolve:setVisible(false);
    end
    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { nameLabel, effectLabel, btnResolve});

    -- 适配角标
    AlignM.adaptSubBg(self.node);
end

-- 适配
function UISpellOperation:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UISpellOperation:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISpellOperation");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册分解按钮点击事件
    local btnResolve = findChildByName(node, "CT/btn_resolve");
    local function onResolveClick(sender, eventType)

        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            self:showResolveConfirm(self.classId);
        end
    end

    btnResolve:addTouchEventListener(onResolveClick);
    TextStyleM.setTextStyle(btnResolve, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnResolve:setTitleText(getLocStr("btn_text_resolve"));

    if self.onlyInfo == true then
        -- 防止点击事件被吞掉
        local bg = findChildByName(self.node, "CT/bg");
        for i = 1, 4 do
            local subBg = findChildByName(bg, "bg" .. i);
            if subBg ~= nil then
                subBg:setSwallowTouches(false);
            end
        end

        -- 注册背景点击事件
        local bgImg = findChildByName(self.node, "bg/bgImg");
        local function onBgClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                -- 关闭当前界面
                AudioM.playFx("button_return");
                UIMgr.getCurrentScene():removeFormByName("UISpellOperation");
            end
        end
        bgImg:addTouchEventListener(onBgClick);
    end
end

function UISpellOperation:showResolveConfirm(classId)
    local ownAmount = ItemM.getAmount(ME.user, classId);

    -- 分解全部
    local splitAll = function()
        DungeonActionM.go("split_spell", classId, ownAmount);

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UISpellOperation");
    end

    -- 分解1个
    local splitOne = function()
        DungeonActionM.go("split_spell", classId, 1);

        -- 拆完了，关闭当前界面
        if ItemM.getAmount(ME.user, classId) < 1 then
            UIMgr.getCurrentScene():removeFormByName("UISpellOperation");
        end
    end

    local title = getLocStr("spell_split_title");
    local itemName = ItemM.query(classId, "name");
    local gainMp = FormulaM.invoke("CALC_SPLIT_SPELL_GAIN_MP", classId);

    if ownAmount == 1 then
        -- 只有一个
        local msg = string.format(getLocStr("split_spell_confirm"), itemName, gainMp);
        local uiConfirm = confirm2(CONFIRM_TYPE_OK, title, msg, splitOne);
    else
        -- 有多个
        local msg = string.format(getLocStr("split_spell_confirm2"), itemName, gainMp,
            gainMp * ownAmount);
        local splitAllText = string.format(getLocStr("btn_text_split"), ownAmount);
        local splitOneText = string.format(getLocStr("btn_text_split"), 1);

        if getLang() ~= "zh" and getLang() ~= "tw" then
            splitAllText = getLocStr("btn_text_all");
            splitOneText = getLocStr("btn_text_one");
        end

        local uiConfirm = confirm2(CONFIRM_TYPE_OK_CANCEL, title, msg,
            splitAll,
            splitOne,
            splitAllText,
            splitOneText);
    end
end
